package com.nail.api.user.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.nail.api.user.bean.User;
import com.nail.api.user.service.IUserService;
import com.nail.client.RedisClient;
import com.nail.common.Constant;
import com.nail.controller.BaseController;
import com.nail.utils.IPUtils;
import com.nail.utils.PageResult;
import com.nail.utils.Result4API;

@Controller
@RequestMapping("/api/user")
public class UserController  extends BaseController {
	Logger logger = Logger.getLogger(this.getClass());

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Autowired
	private RedisClient redisClient;

	@Autowired
	private IUserService userService;

	@PostMapping(value="/getUserByCondition")
	@ResponseBody
	public Result4API getUser(HttpServletRequest request,String token,String condition,Integer pageSize,Integer pageNo){
		Result4API result = new Result4API();
		logger.info(sdf.format(new Date()) +" "+IPUtils.getRemoteHost(request) + ",用户访问了本接口：getUser" );

		try {
			if(StringUtils.isEmpty(token)){
				result.setCode(Constant.API_CODE_501);
				result.setMsg(Constant.API_MSG_501);
				return result;
			}

			User user = (User)redisClient.get(Constant.REDIS_USER_KEY + token);

			PageResult pageResult = new PageResult();
			if(pageNo != null){
				pageResult.setPageNo(pageNo);
			}
			if(pageSize != null){
				pageResult.setPageSize(pageSize);
			}

            result.setCode(Constant.API_CODE_SUCCESS);
            result.setMsg(Constant.API_MSG_SUCCESS);
			if(user != null && user.getRole().getRoleType() == 1) {
				PageResult page = userService.getUserByCondition(pageResult, condition);
				result.setData(page);
			}
			return result;
		} catch (Exception e) {
			logger.error(e.toString());
			e.printStackTrace();
			result.setCode(Constant.API_CODE_ERROR);
			result.setMsg(Constant.API_MSG_ERROR);
			return result;
		}
	}
	
	@GetMapping("/getResult")
    @ResponseBody
    public Result4API getResult(HttpServletRequest request,String token) {

        Result4API result = new Result4API();
        logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：getResult" );
        if(StringUtils.isEmpty(token)){
            result.setCode(Constant.API_CODE_501);
            result.setMsg(Constant.API_MSG_501);
            return result;
        }
        try {
            User user = (User) redisClient.get(Constant.REDIS_USER_KEY + token);
            result.setCode(Constant.API_CODE_SUCCESS);
            result.setMsg(Constant.API_MSG_SUCCESS);
            if(user.getRole().getRoleType() != 1){
            	return result;
            }
            Map<String, Object> map = userService.getResult();
            result.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.toString());
            result.setCode(Constant.API_CODE_501);
            result.setMsg(Constant.API_MSG_ERROR);
        }
        return result;
    }

}
